# -*- coding:utf-8 -*-
import ctypes
import threading
import MySQLdb
import os
import socket
import json
import httplib
import urllib2
import urllib
import uuid
import zipfile
import base64
import sys
import hashlib
import datetime
import time
import subprocess
import xml.dom.minidom
from biplist import *

db_host   = "192.168.11.6"
db_user   = 'xyzs'
db_passwd = 'ikF5NNeJg2G8'
db_port   = 3309
db_name   = "app_xyapplist"

def commom_log(filename, content , write_type='wb'):
    try:
        f = open(filename , write_type)
        f.write(content);
        f.close()
    except:
        print('happen an exception when write file')
        return ""

def get_version(random_filename):
    # try:
    zf       = zipfile.ZipFile(random_filename, "r")
    nList    = zf.namelist()
    type_val = 0

    for f in nList:
        if f.rfind('Info.plist') != -1:
            # print f + "\n"
            f_arr = f.split("/")

            if len(f_arr) == 3:
                if(f_arr[2] == 'Info.plist'):
                    # print f + "\n"
                    info  = zf.read(f)
                    plist = readPlistFromString(info)
                    # print plist
                    # print plist['UIDeviceFamily']
                    if len(plist['UIDeviceFamily']) == 1:
                        type_val = plist['UIDeviceFamily'][0]
                    elif len(plist['UIDeviceFamily']) > 1:
                        type_val = 3
    return type_val
    # except:
    #     return 0

plist = get_version('D:/wamp/www/xyzs/admin.xyzs.com/upscript/1.ipa')
print plist

# arr  = '0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f'
# arr  = arr.split(",")
# conn = MySQLdb.connect(host=db_host,user=db_user,passwd=db_passwd,port=db_port,init_command="set names utf8")
# conn.select_db(db_name)
# cur  = conn.cursor()
# count = 0
# for x in arr:
#     for xx in arr :
#         tb_name = 'app_xyapplist_'+str(x)+".t_xyapplist_"+x+xx

#         sql  = "select itunesid , equipmenttype from "+tb_name+" where is_install = 1";

#         cur.execute(sql)
#         result = cur.fetchall()
#         for f in result:

#             itunesid      = f[0]
#             equipmenttype = f[1]

#             print itunesid

#             m = hashlib.md5(str(itunesid)) 
#             m.digest()
#             md5  = m.hexdigest()
#             str1 = md5[0: 2]
#             str2 = md5[2: 4]

#             random_filename = '/data/app/ipas/'+ str1 + "/" + str2 + "/" + str(md5) + str2 + str1 + '.ipa'

#             if os.path.exists(random_filename):
#                 print random_filename
#                 type_val = get_version(random_filename)
#                 if type_val > 0:
#                     if type_val != equipmenttype:
#                         sql = 'update ' + tb_name +' set equipmenttype = '+str(type_val)+' where itunesid='+str(itunesid)+' limit 1;'
#                         print sql + "\n"
#                         cur.execute(sql)
#                         commom_log('equipmenttype_log.log' , 'itunesid:' + str(itunesid) + "--db_equipmenttype:-" + str(equipmenttype) + "-file_equipmenttype:-" + str(type_val) + "\n" , "a")
#                 else:
#                     commom_log('equipmenttype_error_log.log' , 'itunesid:' + str(itunesid) + "--path:-" + random_filename + "\n" , "a")
#             else:
#                 commom_log('not_is_install_log.log' , 'itunesid:' + str(itunesid) + "--path:-" + random_filename + "\n" , "a")

#         # sys.exit()

# cur.close()
# conn.close()
